using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tomahawk.Runtime.Rendering;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Screen.Controls
{
    /// <summary>
    /// This class represent a simple textured 2d tile, 
    /// commonly used in screen controls
    /// </summary>
    public class TexturedTile : ScreenControl
    {
        //public event Shader.ShaderParameterMethod OnSetShaderParameters;
        protected Texture2D texture;
        //protected Shader shader;
        protected Color modColor = Color.White;
        private string textureAsset;
        private string shaderAsset;
        private float alphaFactor = 1;

        /// <summary>
        /// Constructor, receive all necesary assets
        /// </summary>
        /// <param name="textureAsset">The texture asset that will be used</param>
        /// <param name="shaderAsset">The shader effect that will be used</param>
        public TexturedTile(string textureAsset, string shaderAsset)
            : base()
        {
            this.textureAsset = textureAsset;
            this.shaderAsset = shaderAsset;
        }

        /// <summary>
        /// Load the texture and the asset
        /// </summary>
        protected override void OnLoadGraphicContent(GraphicsDevice myDevice, ContentManager myLoader)
        {
            //load shader
            //shader = new Shader(shaderAsset);
            //shader.LoadGraphicsContent(myDevice, myLoader);

            //load the texture
            this.texture = myLoader.Load<Texture2D>(textureAsset);
        }

        /// <summary>
        /// Render the textured asset, using the shader
        /// </summary>
        protected override void OnRender(GraphicsDevice device, SpriteBatch spriteBatch)
        {
            //try to aply the alpha shader
            //if (this.shader.Effect.Parameters["AlphaFactor"] != null)
            //    this.shader.Effect.Parameters["AlphaFactor"].SetValue(this.alphaFactor);

            //let the client code set the shader parameters
            //if (OnSetShaderParameters != null) OnSetShaderParameters(this.shader.Effect);

            //activate the shader
            //shader.Effect.Begin();
            /*
            // Loop through each pass in the effect like we do elsewhere 
            foreach (EffectPass pass in shader.Effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                //render the textured tile
                DrawSpriteUtil(spriteBatch, this.texture);

                pass.End();
            }

            shader.Effect.End();*/
        }

        #region "Public Properties"

        public Color ModulationColor
        {
            get { return modColor; }
            set { modColor = value; }
        }

        public float AlphaFactor
        {
            get { return alphaFactor; }
            set { alphaFactor = value; }
        }

        #endregion

        protected void DrawSpriteUtil(SpriteBatch spriteBatch, Texture2D texture)
        {
            spriteBatch.Draw(texture, this.Rect, null, this.modColor, 0, Vector2.Zero, SpriteEffects.None, this.LayerDepth);
        }

    }

}


